/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *  02110-1301, USA.
 * 
 * $Id: SpellMagicMissile.java 48 2008-05-17 02:04:48Z jens464 $
*/
package albareth.core.action;


import frosch.util.FToolkit;
import albareth.core.GameEffect;
import albareth.core.GameEffectObserver;
import albareth.core.entity.Mobile;
import albareth.frontend.Resources;
import albareth.frontend.TileCollection;
import albareth.util.Location;

public class SpellMagicMissile extends Spell implements GameEffectObserver
{
    private Mobile _caster = null;


    public SpellMagicMissile()
    {
        super("Magic Missile", TileCollection.ICON_MAGICMISSILE);
        setManaRequirement(2);
        setCastingDuration(0);
        setCooldownDuration(1000);
        setRange(8);
        setFlags(HARMFUL|TARGET_LOC|TARGET_NOSELF);
    }

    protected boolean onExecute(Mobile src)
    {
        GameEffect effect = GameEffect.flying(
                Resources.EFFECT_MAGICMISSILE,
                src.getLocation(), internalTargetLocation, 10f,
                false,
                GameEffect.HIT_ITEMS_ON_PATH |
                GameEffect.HIT_ENEMIES_ON_PATH |
                GameEffect.FLY_INFINITE
        );
        effect.setObserver(this);
        src.emitEffect(effect);
        
        src.emitSound(Resources.SOUND_MAGIC_MISSILE);
        
        _caster = src;
        
        return true;
    }

    protected boolean onInitiate(Mobile src)
    {
        return true;
    }

    /**
     * Fireball explosion.
     */
    public void effectFinished(GameEffect effect)
    {
        int missileDamage = FToolkit.rand(4, 8) + (getSpellStrength()-1) * 3;
        int damageType = 0; //TODO: damage type fire
        
        Location exploLoc = effect.getCurrentLocation();
        
        GameEffect exploEffect = GameEffect.stationary(
                Resources.EFFECT_MAGICEXPLOSION, exploLoc, -1
        );
        _caster.emitEffect(exploEffect);
        _caster.emitSound(Resources.SOUND_EXPLO_MAGIC);
        
        //direct hit explode cell:
        exploLoc.inflictDamage(
                Math.round(missileDamage), damageType, _caster);
    }

}
